package com.twu.biblioteca.menu;

import com.twu.biblioteca.exceptions.InvalidMenuSelectionException;

import java.io.InputStream;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * User: cschlumb
 * Date: 07/08/2013
 * Time: 10:18
 * To change this template use File | Settings | File Templates.
 */
public class SelectionParser {

    private final InputStream inputStream;

    public SelectionParser(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    public Menu parseInput() throws InvalidMenuSelectionException{
        try {
            Scanner s = new Scanner(inputStream).useDelimiter("\n");
            int selection = s.nextInt();

            return Menu.values()[selection];
        } catch (ArrayIndexOutOfBoundsException e){
            throw new InvalidMenuSelectionException();
        } catch (InputMismatchException e){
            throw new InvalidMenuSelectionException();
        }
    }

}
